package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ContractPaymentMapper;
import com.ruoyi.system.domain.ContractPayment;
import com.ruoyi.system.service.IContractPaymentService;

/**
 * 合同收款-CRUDService业务层处理
 *
 * @author ruoyi
 * @date 2022-08-02
 */
@Service
public class ContractPaymentServiceImpl implements IContractPaymentService
{
    @Autowired
    private ContractPaymentMapper contractPaymentMapper;

    /**
     * 查询合同收款-CRUD
     *
     * @param paymentId 合同收款-CRUD主键
     * @return 合同收款-CRUD
     */
    @Override
    @DataScope(deptAlias = "cp",userAlias = "cp")
    public ContractPayment selectContractPaymentByPaymentId(Long paymentId)
    {
        return contractPaymentMapper.selectContractPaymentByPaymentId(paymentId);
    }

    /**
     * 查询合同收款-CRUD列表
     *
     * @param contractPayment 合同收款-CRUD
     * @return 合同收款-CRUD
     */
    @Override
    @DataScope(deptAlias = "cp",userAlias = "cp")
    public List<ContractPayment> selectContractPaymentList(ContractPayment contractPayment)
    {
        return contractPaymentMapper.selectContractPaymentList(contractPayment);
    }

    /**
     * 新增合同收款-CRUD
     *
     * @param contractPayment 合同收款-CRUD
     * @return 结果
     */
    @Override
    @DataScope(deptAlias = "cp",userAlias = "cp")
    public int insertContractPayment(ContractPayment contractPayment)
    {
        return contractPaymentMapper.insertContractPayment(contractPayment);
    }

    /**
     * 修改合同收款-CRUD
     *
     * @param contractPayment 合同收款-CRUD
     * @return 结果
     */
    @Override
    public int updateContractPayment(ContractPayment contractPayment)
    {
        return contractPaymentMapper.updateContractPayment(contractPayment);
    }

    /**
     * 批量删除合同收款-CRUD
     *
     * @param paymentIds 需要删除的合同收款-CRUD主键
     * @return 结果
     */
    @Override
    public int deleteContractPaymentByPaymentIds(Long[] paymentIds)
    {
        return contractPaymentMapper.deleteContractPaymentByPaymentIds(paymentIds);
    }

    /**
     * 删除合同收款-CRUD信息
     *
     * @param paymentId 合同收款-CRUD主键
     * @return 结果
     */
    @Override
    public int deleteContractPaymentByPaymentId(Long paymentId)
    {
        return contractPaymentMapper.deleteContractPaymentByPaymentId(paymentId);
    }
    @Override
    public String importPayment(List<ContractPayment> contractPaymentList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(contractPaymentList) || contractPaymentList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<ContractPayment> existList = selectContractPaymentList(null);
        for (ContractPayment importData : contractPaymentList)
        {
            try {

                boolean userFlag = false;
                for (ContractPayment entry : existList) {
                    if (entry.getContractNo().equals(importData.getContractNo())) {
                        userFlag = true;
                        break;
                    }
                }
                if (!userFlag) {
                    insertContractPayment(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getContractNo() + " 导入成功");
                } else if (isUpdateSupport) {
                    updateContractPayment(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getContractNo() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + importData.getContractNo() + " 已存在");
                }
            }catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + importData.getContractNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
